package com.xiaomili.halfsugar.fragment;


import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import com.google.gson.Gson;
import com.xiaomili.halfsugar.R;
import com.xiaomili.halfsugar.bean.CategoryBean;
import com.xiaomili.halfsugar.constant.UrlString;
import com.xiaomili.halfsugar.utils.LogUtils;
import com.xiaomili.halfsugar.utils.OKHttpUtils;
import com.xiaomili.halfsugar.utils.OKHttpUtils.MyCallback;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link ProductFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ProductFragment extends Fragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;

    private Activity mActivity;
    private List<Fragment> mDatas;


    @InjectView(R.id.vp_category)
    ViewPager mVpCategory;
    @InjectView(R.id.rg_container)
    RadioGroup mRgContainer;
    @InjectView(R.id.recycler_hot_recommand)
    RecyclerView mRecyclerHotRecommand;

    public ProductFragment() {
        // Required empty public constructor
    }

    public static ProductFragment newInstance(String param1, String param2) {
        ProductFragment fragment = new ProductFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mDatas = new ArrayList<>();
        mActivity = getActivity();
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_product, container, false);
        ButterKnife.inject(this, view);
        initData();
        return view;
    }

    private void initData() {
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //加载Category的数据
        loadCategoryData();
        //加载HotRecommand的数据
        loadHotRecommandData();
    }


    //加载数据
    private void loadCategoryData() {
        OKHttpUtils.newInstance(mActivity).obtainDataAsyncByGet(mParam1, new MyCallback() {
            @Override
            public void onFailure(IOException e) {
                LogUtils.i("网络加载失败");

            }

            @Override
            public void onResponse(String result) {
                CategoryBean categoryBean = new Gson().fromJson(result, CategoryBean.class);
            }
        });
    }

    //加载HotRecommand的数据
    private void loadHotRecommandData() {
        OKHttpUtils.newInstance(mActivity).obtainDataAsyncByGet(UrlString.COMMUNITY_PRODUCT_CATEGORY, new MyCallback() {
            @Override
            public void onFailure(IOException e) {

            }

            @Override
            public void onResponse(String result) {
//                HotRecmmandBean hotRecmmandBean = new Gson().fromJson(result, HotRecmmandBean.class);
//                mRecyclerHotRecommand.setLayoutManager(new GridLayoutManager(mActivity,3));
//                mRecyclerHotRecommand.setAdapter(new CommonRecyclerViewAdapter<DataBean>(mActivity, hotRecmmandBean.data, R.layout.item_recycler_com_product_hot_rcmd) {
//                    @Override
//                    public void bindData(CommonHolder holder, int position, DataBean bean) {
//                        ImageView imageView = holder.findViewById(R.id.iv_item);
//                        Picasso.with(mActivity).load(bean.mini_pic_url).into(imageView);
//                    }
//                });
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }
}
